package com.ka.magicexplorer.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ka.magicexplorer.provider.MySession;
import com.ka.magicexplorer.provider.MySession.MenuState;

public class ListFunctActivity extends Activity {

	protected LinearLayout listService;
	protected ImageView imvDocument;
	protected ImageView imvGallery;
	protected ImageView imvMedia;

	protected static long back_pressed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_funct);

		init();
		execute();
	}

	private void init() {

		listService = (LinearLayout) findViewById(R.id.containerService);
		imvDocument = (ImageView) findViewById(R.id.goto_document);
		imvGallery = (ImageView) findViewById(R.id.goto_gallery);
		imvMedia = (ImageView) findViewById(R.id.goto_media);

	}

	private void execute() {
		changeConfiguration();

		imvDocument.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setFocus(MySession.MenuState.documentState);
				startActivity(new Intent(ListFunctActivity.this,
						DocumentActivity.class));
			}
		});

		imvGallery.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setFocus(MySession.MenuState.galleryState);
				startActivity(new Intent(ListFunctActivity.this,
						GalleryActivity.class));
			}
		});

		imvMedia.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setFocus(MySession.MenuState.mediaState);
				startActivity(new Intent(ListFunctActivity.this,
						MediaActivity.class));
			}

		});

	}
	
	private void setFocus(MenuState state) {
		MySession.getInstance().setCurrentGroupFocus(state);
		MySession.getInstance().setCurrentChildFocus(MySession.MenuState.noState);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		changeConfiguration();
	}

	private void changeConfiguration() {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			listService.setOrientation(LinearLayout.VERTICAL);
		} else {
			listService.setOrientation(LinearLayout.HORIZONTAL);
		}

	}

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis())
			moveTaskToBack(true);
		else
			Toast.makeText(getBaseContext(),
					getResources().getString(R.string.close_app),
					Toast.LENGTH_SHORT).show();
		back_pressed = System.currentTimeMillis();
	}

}
